
import random
import time
from selenium import webdriver
from selenium.webdriver import ActionChains

def get_track(offset):
    """
    通过偏移总量,模拟操作
    :param offset:
    :return:
    """
    # 步伐
    track = list()
    # 当前位移
    current = 0
    # 中间点 ,切换加速度
    mid = offset * 0.5
    t = 0.5
    v = 0
    while current < offset:
        print(current)
        # if current < round(mid):
        #     a = 1
        # else:
        #     a = -1
        a = 1.1
        v0 = v
        v = v0 + a * t
        move = v0 * t + 1 / 2 * a * (t ** 2)
        current += move
        track.append(round(move))
        # track.append(move)
    return track

def moni():
    driver = webdriver.Chrome()
    driver.get("https://e.waimai.meituan.com/new_fe/login_gw#/login")
    time.sleep(2)
    iframe = driver.find_element_by_xpath('//*[@id="wm-logon"]/div[2]/iframe')
    driver.switch_to.frame(iframe)
    try:
        # driver.find_element_by_xpath('//*[@id="login"]').send_keys("python-linming")
        driver.find_element_by_id("login").send_keys("python-linming")
        driver.find_element_by_id("password").send_keys("1234")
        driver.find_element_by_xpath('//*[@id="login-form"]/button').click()
    except:
        driver.close()
    time.sleep(5)
    button = driver.find_element_by_xpath('//*[@id="yodaBox"]')
    track = get_track(200)
    while True:
    # ActionChains(driver).move_by_offset(xoffset=10, yoffset=0).perform()
        ActionChains(driver).click_and_hold(button).perform()
        for i in track:
            print(i)
            # ActionChains(driver).move_by_offset(xoffset=-0.5, yoffset=0).perform() if i %2==0 else None
            ActionChains(driver).move_by_offset(xoffset=i, yoffset=random.randint(1,3)).perform()
        time.sleep(1)
        ActionChains(driver).release().perform()
        time.sleep(4)

    driver.close()

def moni1():
    import requests
from bs4 import BeautifulSoup
import re
 
def login_meituan(username, password):
    session = requests.Session()
    url = 'https://passport.waimai.meituan.com/login'
    
    # 获取登录页面的cookies和一些初始参数
    response = session.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    
    # 提取表单的action和隐藏的输入字段
    action = soup.find('form', {'id': 'loginForm'})['action']
    data = {
        'username': username,
        'password': password,
        'remember_me': 'on',  # 如果有记住我选项的话
        'service': 'https://passport.waimai.meituan.com/sso/login'  # 通常这是登录后重定向的URL
    }
    
    # 提取可能的动态生成的token或者其他隐藏字段（如果有的话）
    for input_tag in soup.find_all('input', {'type': 'hidden'}):
        data[input_tag['name']] = input_tag['value']
    
    # 使用session发送登录请求
    login_response = session.post(action, data=data)
    
    # 检查登录是否成功（这里可以根据需要检查返回的HTML或者状态码）
    if login_response.ok:
        print("登录成功")
        # 可以进一步处理，例如获取用户信息等
    else:
        print("登录失败")
        print(login_response.text)  # 打印错误信息或者调试信息
    
    return session

